/*
   Class that defines a standard ratings component.
*/

// Messages for display above the individual ratings units. The length of the
// array also defines how many units to include in the component.
yg_Ratings.Msgs = new Array
(
   "Awful",
   "Poor",
   "Average",
   "Good",
   "Excellent"
);

yg_Ratings.Labels = new Array
(
   "1 Star",
   "2 Stars",
   "3 Stars",
   "4 Stars",
   "5 Stars"
);

// Path for all images.
//var path = "http://us.i1.yimg.com/us.yimg.com/i/us/ls/gr/";
var path = "http://us.i1.yimg.com/us.yimg.com/i/us/sh/karma/yri_";
yg_Ratings.starbar = "star_";

// Image for set units.
yg_Ratings.UnitY = "yellow.gif";

// Image for set units <= the mouse over point.
yg_Ratings.UnitYMouseOver = "yellow.gif";

// Image for set units > the mouse over point.
yg_Ratings.UnitYMouseLess = "grey.gif";

// Image for unset units.
yg_Ratings.UnitN = "white.gif";

// Image for unset units <= the mouse over point.
yg_Ratings.UnitNMouseOver = "hover.gif";

function yg_Ratings(id,flabel,defaultval,type)
{
   // The id parameter is the name (a string) of the variable to which the
   // instance is assigned. (The variable is sent along to event handlers,
   // so it must be in the global scope.)
   var i, t;
   var attributes;
   var h1, h2;
   var d = document;
   var style;   
   this.starbar = type + "_";

   this.rating = 0;
   this.defaultMsg = "Rate it:";
  
   if (defaultval > 0) 
       this.defaultMsg = yg_Ratings.Msgs[defaultval-1];

   if(flabel){
    	d.write('<p class="ylsclear">');
     	d.write('<span class="ylsflabel"');
        if (type == "star")
			d.write('style="font-weight: bold; margin-top: 1.5em; width: 160px;"');
		else
			d.write('style="margin-left: 20px;"');
		d.write('>' + flabel + ':</span>');
//        style = "float:left;vertical-align:top;width:94px;"; //if ratings used in forms add css
   }
   attributes = 'class="ygrtngs" id="' + id + '" style="' + style + '"';
   h1 = 'onMouseOut="return yg_Ratings_mouseOut(' + id + ');"';

   d.write('<span ' + attributes + ' ' + h1 + '>');
   if (type == "star")
      d.write('<div class="msg"><small>'+this.defaultMsg+'</small></div>');

   for (i = 1; i <= yg_Ratings.Msgs.length; i++)
   {
      h1 = 'onMouseOver="return yg_Ratings_mouseOver(' + id + ', ' + i + ');"';
      h2 = 'onClick="return yg_Ratings_click(' + id + ', ' + i + ');"';
      d.write('<span class="unit "' + h1 + ' ' + h2 + '>');
      if (i <= defaultval) {
         d.write('<img src="' + path + this.starbar + yg_Ratings.UnitY + '" />');
      } else {
         d.write('<img src="' + path + this.starbar + yg_Ratings.UnitN + '" />');
      }
      d.write('</span>');
   }
   if(defaultval){
	d.write('<input type="hidden" name="' +id+  '_" value="' + defaultval + '" />');
   } else {
	d.write('<input type="hidden" name="' +id+  '_" value="" />');    
   }

  d.write('</span>');
   if(flabel)d.write('</p>');
 
   this.parent = document.getElementById(id);
   this.images = this.parent.getElementsByTagName("img");
   //this.field = this.parent.getElementsByTagName("input");
   if (type == "star")
       this.field = this.parent.childNodes[6];
   else
       this.field = this.parent.childNodes[5];
   this.msg = this.parent.getElementsByTagName("small")[0];
   this.id = id;
}

function yg_Ratings_set(n, oflag)
{
   // The n parameter is the unit to set (starting at 1). Set oflag to true
   // when the mouse is outside of the ratings component, or you're not sure.
   if (arguments.length < 2)
      oflag = true;   
   this.rating = n;
   this.defaultMsg = yg_Ratings.Msgs[n-1];
   this.update(n, oflag);
   //if((THIS_PAGE=="details")||(THIS_PAGE=="readreviews")) yls_Ratings_showSubmit('btnSave'); 
   this.field.value = n;
//   if (THIS_PAGE=="details"
//   || THIS_PAGE=="readreviews") {
   if (document.getElementById('btnSave')) {
      yls_Ratings_showSubmit('btnSave');
      log_quickrate_beacon();
   }
}

function yls_Ratings_showSubmit(sBtn) {
	document.getElementById(sBtn).style.display="block";
}


function yg_Ratings_setMsg(m)
{
   var children = this.msg.childNodes;
   var node;

   for (var i = 0; i < children.length; i++)
   {
      node = children[i];

      if (node.nodeType == 3)
      {
         // Using 0xA0 prevents the browser from collapsing empty messages.
         if (m == "")
            node.nodeValue = this.defaultMsg;
         else
            node.nodeValue = m;
      }
   }
}

function yg_Ratings_get()
{
   return this.rating;
}

function yg_Ratings_update(n, oflag)
{
   // The oflag parameter is true when the mouse is outside of the ratings
   // component. The n parameter is the 
   if (this.starbar == 'star_') {
	   if (oflag) {
		  this.setMsg(this.defaultMsg);
	   } else {
		  this.setMsg(yg_Ratings.Msgs[n - 1]);
		  //if(event.type=="click") this.field.value=n;
	   }
   }

   for (i = 1; i <= yg_Ratings.Msgs.length; i++)
   {
      if (oflag)
      {
         if (i <= this.rating)
            this.images[i - 1].src = path + this.starbar + yg_Ratings.UnitY;
         else
            this.images[i - 1].src = path + this.starbar + yg_Ratings.UnitN;
      }
      else
      {
         if (i <= n)
         {
            if (i <= this.rating)
               this.images[i - 1].src = path + this.starbar + yg_Ratings.UnitYMouseOver;
            else
               this.images[i - 1].src = path + this.starbar + yg_Ratings.UnitNMouseOver;
         }
         else
         {
            if (i <= this.rating)
               this.images[i - 1].src = path + this.starbar + yg_Ratings.UnitYMouseLess;
            else
               this.images[i - 1].src = path + this.starbar + yg_Ratings.UnitN;
         }
      }
   }
   return true;
}

function yg_Ratings_click(obj, n)
{
   obj.set(n, false);
   return true;
}

function yg_Ratings_mouseOver(obj, n)
{
   obj.update(n, false);
   return true;
}

function yg_Ratings_mouseOut(obj)
{
   obj.update(0, true);
   return true;
}

yg_Ratings.prototype.set = yg_Ratings_set;
yg_Ratings.prototype.setMsg = yg_Ratings_setMsg;
yg_Ratings.prototype.get = yg_Ratings_get;
yg_Ratings.prototype.update = yg_Ratings_update;